unit TerrainProperties;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.Variants,
  System.SysUtils,
  System.Classes,
  Vcl.StdCtrls,
  Vcl.Graphics,
  Vcl.Forms,
  Vcl.ExtDlgs,
  Vcl.Dialogs,
  Vcl.Controls,
  Vcl.Buttons,
   
  GLS.Texture,
  GLS.Material,
  GLS.VectorGeometry;

type
  TTerrainPropsForm = class(TForm)
    Label14: TLabel;
    Label17: TLabel;
    VScaleEdit: TEdit;
    HScaleEdit: TEdit;
    SpeedButton2: TSpeedButton;
    LODCB: TComboBox;
    Thresholdcb: TComboBox;
    Label6: TLabel;
    Label8: TLabel;
    TerrainMatnameEdit: TEdit;
    Label3: TLabel;
    TerrainMatNameButton: TSpeedButton;
    Label1: TLabel;
    TerrainDetailNameButton: TSpeedButton;
    TerrainDetailNameEdit: TEdit;
    procedure SpeedButton2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure HScaleEditKeyPress(Sender: TObject; var Key: Char);
    procedure LODCBKeyPress(Sender: TObject; var Key: Char);
    procedure LODCBClick(Sender: TObject);
    procedure ThresholdcbClick(Sender: TObject);
    procedure TerrainMatNameButtonClick(Sender: TObject);
    procedure TerrainMatnameEditChange(Sender: TObject);
    procedure TerrainDetailNameEditChange(Sender: TObject);
    procedure TerrainDetailNameButtonClick(Sender: TObject);
  private
     
  public
     
  end;

var
  TerrainPropsForm: TTerrainPropsForm;

implementation

uses
  NodeProperties,
  main,
  MatSelect;

{$R *.dfm}

procedure TTerrainPropsForm.FormShow(Sender: TObject);
begin
  HScaleEdit.text := format('%.4f', [form1.terrain.hscale]);
  VScaleEdit.text := format('%.4f', [form1.terrain.vscale]);
  TerrainMatnameEdit.text := form1.terrain.texname;
  TerrainDetailNameEdit.text := form1.terrain.detailname;

  case form1.terrain.LOD of
    1:
      LODCB.itemindex := 0;
    2:
      LODCB.itemindex := 1;
    3:
      LODCB.itemindex := 2;
    8:
      LODCB.itemindex := 3;
    16:
      LODCB.itemindex := 4;
    32:
      LODCB.itemindex := 5;
    64:
      LODCB.itemindex := 6;
  end;
  Thresholdcb.itemindex := form1.terrain.threshold;
end;

procedure TTerrainPropsForm.HScaleEditKeyPress(Sender: TObject; var Key: Char);
begin
  NodePropsForm.RoadOffsetEditKeyPress(Sender, Key);
end;

procedure TTerrainPropsForm.SpeedButton2Click(Sender: TObject);
var
  i, j: integer;
  libmat: TGLLibMaterial;
begin
  case LODCB.itemindex of
    0:
      form1.terrain.LOD := 1;
    1:
      form1.terrain.LOD := 2;
    2:
      form1.terrain.LOD := 4;
    3:
      form1.terrain.LOD := 8;
    4:
      form1.terrain.LOD := 16;
    5:
      form1.terrain.LOD := 32;
    6:
      form1.terrain.LOD := 64;
  end;
  form1.terrain.threshold := Thresholdcb.itemindex;
  form1.terrain.hscale := strtofloat(HScaleEdit.text);
  form1.terrain.vscale := strtofloat(VScaleEdit.text);
  form1.terrain.texname := TerrainMatnameEdit.text;
  form1.terrain.detailname := TerrainDetailNameEdit.text;
  form1.terrain.structurechanged;

  for i := 0 to form1.GLMaterialLibrary1.Materials.Count - 1 do
  begin
    form1.GLMaterialLibrary1.Materials[i].Texture2Name := '';
    form1.GLMaterialLibrary1.Materials[i].Material.Texture.imagebrightness := 1;
  end;

  libmat := form1.GLMaterialLibrary1.LibMaterialByName(form1.terrain.texname);
  if assigned(libmat) then
  begin
    libmat.Texture2Name := TerrainDetailNameEdit.text;
    libmat := form1.GLMaterialLibrary1.LibMaterialByName(libmat.Texture2Name);
    if assigned(libmat) then
    begin
      libmat.TextureScale.AsAffineVector :=
        affinevectormake(form1.terrain.hscale * 8, form1.terrain.hscale * 8, 0);
      libmat.Material.Texture.imagebrightness := 2;
    end;
  end;

  with form1.freeform2 do
    for i := 0 to meshobjects.Count - 1 do
      with meshobjects[i] do
        for j := 0 to facegroups.Count - 1 do
        begin
          facegroups[j].MaterialName := form1.terrain.texname;
        end;
  form1.freeform2.structurechanged;

  form1.glsceneviewer1.refresh;
end;

procedure TTerrainPropsForm.LODCBKeyPress(Sender: TObject; var Key: Char);
begin
  Key := #0;
end;

procedure TTerrainPropsForm.LODCBClick(Sender: TObject);
begin
  LODCB.Enabled := false;
  LODCB.Enabled := true;
end;

procedure TTerrainPropsForm.ThresholdcbClick(Sender: TObject);
begin
  Thresholdcb.Enabled := false;
  Thresholdcb.Enabled := true;
end;

procedure TTerrainPropsForm.TerrainMatNameButtonClick(Sender: TObject);
begin
  if matselectform.ShowModal = mrOK then
    if matselectform.ListBox1.itemindex > -1 then
      TerrainMatnameEdit.text := matselectform.ListBox1.Items
        [matselectform.ListBox1.itemindex]
    else
      TerrainMatnameEdit.text := '';
end;

procedure TTerrainPropsForm.TerrainMatnameEditChange(Sender: TObject);
begin
  if TerrainMatnameEdit.text = '' then
    TerrainMatnameEdit.text := 'null';
  if Sender = nil then
    form1.terrain.texname := TerrainMatnameEdit.text;
end;

procedure TTerrainPropsForm.TerrainDetailNameEditChange(Sender: TObject);
begin
  if TerrainDetailNameEdit.text = '' then
    TerrainDetailNameEdit.text := 'null';
  if Sender = nil then
    form1.terrain.detailname := TerrainDetailNameEdit.text;
end;

procedure TTerrainPropsForm.TerrainDetailNameButtonClick(Sender: TObject);
begin
  if matselectform.ShowModal = mrOK then
    if matselectform.ListBox1.itemindex > -1 then
      TerrainDetailNameEdit.text := matselectform.ListBox1.Items
        [matselectform.ListBox1.itemindex]
    else
      TerrainDetailNameEdit.text := '';
end;

end.
